/*
 * 文件名：SuperBudgetOfgroupController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年5月11日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.fia.web;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.fia.domain.SuperBudgetOfgroupDomain;
import top.infopub.mgr.fia.service.SuperBudgetOfgroupService;
import top.infopub.mgr.fia.vo.SuperBudgetOfgroupVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.hr.service.OrgInfoService;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.mgr.util.LogUtil;
import top.infopub.mgr.util.LogUtil.AcctionType;
import top.infopub.mgr.util.LogUtil.LogModelType;
import top.infopub.model.fia.SuperBudgetOfgroup;
import top.infopub.model.fia.SuperBudgetOfgroupDtl;
import top.infopub.model.hr.StaffInfo;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;
import top.infopub.util.IdFactory;

import com.github.pagehelper.Page;


/**
 * 建投总部年度控股指标预算
 * @author wuzhiyang
 * @version 2020年5月11日
 * @see SuperBudgetOfgroupController
 * @since
 */
@Controller
@RequestMapping("/superBudgetOfgroup")
public class SuperBudgetOfgroupController extends GenericFlowController {

    @Autowired
    private StaffInfoService staffInfoService;

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private SuperBudgetOfgroupService superBudgetOfgroupService;

    /**
     * 建投总部年度控股指标预算
     * @return
     */
    @RequestMapping("/list")
    public String list() {
        return "fia/lay-superBudgetOfgroup-list";
    }

    /**
     * 建投总部年度控股指标预算
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<SuperBudgetOfgroupDomain> queryDataByCondition(SuperBudgetOfgroupVo superBudgetOfgroupVo,
                                                                ParamPage paramPage) {
        Page<SuperBudgetOfgroupDomain> page = new Page<SuperBudgetOfgroupDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (superBudgetOfgroupVo.getEndDate() != null) {
            superBudgetOfgroupVo.setEndDate(DateUtils.addDays(superBudgetOfgroupVo.getEndDate(), 1));
        }
        page = superBudgetOfgroupService.selectByDomainWhere(page, superBudgetOfgroupVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 建投总部年度控股指标预算-新增
     * @return
     */
    @RequestMapping("/add")
    public String add(HttpServletRequest request) {
        String bindOrgId = String.valueOf(SessionUserUtils.getSessionAttribute("bindOrgId"));
        String bindOrgName = String.valueOf(SessionUserUtils.getSessionAttribute("bindOrgName"));
        String bindDeptId = String.valueOf(SessionUserUtils.getSessionAttribute("bindDeptId"));
        String bindDeptName = String.valueOf(SessionUserUtils.getSessionAttribute("bindDeptName"));
        request.setAttribute("bindOrgId", bindOrgId);
        request.setAttribute("bindOrgName", bindOrgName);
        request.setAttribute("bindDeptId", bindDeptId);
        request.setAttribute("bindDeptName", bindDeptName);
        AuthorizationVO authorizationVO = SessionUserUtils.getSessionAttributeForUserDtl();
        StaffInfo staffInfo = staffInfoService.selectByPrimaryKey(authorizationVO.getHrStaffId());
        request.setAttribute("staffName", staffInfo.getStaffName());
        return "fia/lay-superBudgetOfgroup-add";
    }

    /**
     * 根据id查询建投总部年度控股指标预算详情  > 注意：方法命名必须为：toDealOrDetail
     * @param id 建投总部年度控股指标预算Id
     * @return
     * @see
     */
    @RequestMapping("/toDealOrDetail")
    public ModelAndView queryById(String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        SuperBudgetOfgroupVo superBudgetOfgroupVo = superBudgetOfgroupService.queryById(id);
        map.put("superBudgetOfgroupVo", superBudgetOfgroupVo);
        return new ModelAndView("fia/lay-superBudgetOfgroup-detail", map);
    }

    /**
     * 新增建投总部年度控股指标预算
     * @return
     */
    @SuppressWarnings({"rawtypes"})
    @RequestMapping("/save")
    public @ResponseBody RspVo save(HttpServletRequest request,
                                    SuperBudgetOfgroupVo superBudgetOfgroupVo) {

        String[] monthDtl = request.getParameterValues("months");//子项月份
        String[] cotFeeDtl = request.getParameterValues("cotFee");//子项-合同费
        String[] manageFeeDtl = request.getParameterValues("manageFee");//子项-管理费用

        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();

        /**
         * 建投总部年度控股指标预算主表处理
         */
        String hrStaffApplyid = request.getParameter("hrStaffApplyid");
        String staffname = request.getParameter("staffname");
        String hrOrgid = request.getParameter("hrOrgid");
        String hrDeptid = request.getParameter("hrDeptid");
        superBudgetOfgroupVo.setHrDeptId(hrDeptid);
        superBudgetOfgroupVo.setHrOrgId(hrOrgid);
        superBudgetOfgroupVo.setStaffName(staffname);
        superBudgetOfgroupVo.setHrStaffApplyId(hrStaffApplyid);
        String id = IdFactory.getUUIDUpper();
        superBudgetOfgroupVo.setId(id);
        superBudgetOfgroupVo.setHrStaffApplyId(vo.getHrStaffId());
        superBudgetOfgroupVo.setAddBy(vo.getId());
        superBudgetOfgroupVo.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
        String codes = codeGeneratorConfigService.generatorCode("fia_super_budget_ofgroup");
        superBudgetOfgroupVo.setCodes(codes);
        superBudgetOfgroupVo.setProcessStatus("9");//生效中
        superBudgetOfgroupVo.setUptBy(vo.getId());
        superBudgetOfgroupVo.setUptTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());

        //子项处理
        List<SuperBudgetOfgroupDtl> superBudgetOfgroupDtls = new ArrayList<>();
        for (int i = 0; i < monthDtl.length; i++ ) {
            String months = monthDtl[i];
            SuperBudgetOfgroupDtl superBudgetOfgroupDtl = new SuperBudgetOfgroupDtl();
            String subId = IdFactory.getUUIDUpper();//自动生成主键
            superBudgetOfgroupDtl.setId(subId);
            superBudgetOfgroupDtl.setSuperBudgetId(id);
            superBudgetOfgroupDtl.setMonths(Integer.parseInt(months));
            superBudgetOfgroupDtl.setManageFee(new BigDecimal(manageFeeDtl[i]));
            superBudgetOfgroupDtl.setCotFee(new BigDecimal(cotFeeDtl[i]));
            superBudgetOfgroupDtl.setAddBy(vo.getId());
            superBudgetOfgroupDtl.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
            superBudgetOfgroupDtls.add(superBudgetOfgroupDtl);
        }

        superBudgetOfgroupService.saveSuperBudgetOfgroup(superBudgetOfgroupVo,
            superBudgetOfgroupDtls);
        LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD, "新增建投总部年度控股指标预算 ", null);
        return RspVo.getSuccessResponse();
    }

    /**
     * 修改 建投总部年度控股指标预算状态
     * @param 
     * @return 
     * @see
     */
    @RequestMapping("/updateStatus")
    public @ResponseBody RspVo<String> updateStatus(String id) {
        SuperBudgetOfgroup superBudgetOfgroup = superBudgetOfgroupService.selectByPrimaryKey(id);
        String oldStatus = superBudgetOfgroup.getProcessStatus();
        String newStatus = "9";
        if ("9".equals(oldStatus)) {
            newStatus = "10";
        }
        else {
            newStatus = "9";
        }
        superBudgetOfgroup.setProcessStatus(newStatus);
        superBudgetOfgroup.setUptTime(new Date());
        superBudgetOfgroup.setUptBy(SessionUserUtils.getSessionUserId());
        superBudgetOfgroupService.updateBySelective(superBudgetOfgroup);
        // 操作日志
        LogUtil.bizLog(LogUtil.LogModelType.OTHER, LogUtil.AcctionType.UPDATE,
            "修改状态   建投总部年度控股指标预算表单据ID:" + superBudgetOfgroup.getCodes(), "");
        return RspVo.getSuccessResponse();
    }

}
